import { login, logout, getInfo } from '@/api/login'
import { getToken, setToken, removeToken, setLoginUser, remokeCathe } from '@/utils/auth'
import store from '../../store'
import router from '../../router'

const user = {
  state: {
    token: getToken(),
    name: '',
    mobile: '',
    avatar: '',
    companyId: '',
    roles: [],
    address: '',
    contact: '',
    companName: '',
    taxIdentificationCode: ''
  },

  mutations: {
    SET_TOKEN: (state, token) => {
      state.token = token
    },
    SET_NAME: (state, name) => {
      state.name = name
    },
    SET_AVATAR: (state, avatar) => {
      state.avatar = avatar
    },
    SET_ROLES: (state, roles) => {
      state.roles = roles
    },
    SET_MOBILE: (state, mobile) => {
      state.mobile = mobile
    },
    SET_COMPANY: (state, companyId) => {
      state.companyId = companyId
    },
    SET_COMPANYNAME: (state, companyName) => {
      state.companyName = companyName
    },
    SET_ADDRESS: (state, address) => {
      state.address = address
    },
    SET_CONTACT: (state, contact) => {
      state.contact = contact
    },
    SET_TAXIDENTIFICATIONCODE: (state, taxIdentificationCode) => {
      state.taxIdentificationCode = taxIdentificationCode
    }
  },

  actions: {
    Company({commit}, comapny) {
      console.log(JSON.stringify(comapny))
      commit('SET_TAXIDENTIFICATIONCODE', comapny.taxIdentificationCode)
      commit('SET_CONTACT', comapny.contact)
      commit('SET_ADDRESS', comapny.address)
      commit('SET_COMPANYNAME', comapny.name)
    },
    ResetRoles({commit}, roles) {
      commit('SET_ROLES', roles)
    },
    // 登录
    Login({ commit }, userInfo) {
      const username = userInfo.username.trim()
      return new Promise((resolve, reject) => {
        login(username, userInfo.password).then(response => {
          const data = response.data
          setToken(data.token)
          store.dispatch('GenerateRoutes', { roles: data.authorities }).then(() => { // 生成可访问的路由表
            router.addRoutes(store.getters.addRouters) // 动态添加可访问路由表
          })
          setLoginUser(response.data)
          localStorage.setItem('username', username)
          commit('SET_TOKEN', data.token)
          commit('SET_MOBILE', username)
          commit('SET_NAME', data.name || data.firstname)
          commit('SET_AVATAR', 'https://wpimg.wallstcn.com/f778738c-e4f8-4870-b634-56703b4acafe.gif')
          commit('SET_MOBILE', data.username)
          commit('SET_COMPANY', data.companyId)
          commit('SET_ROLES', data.authorities)
          localStorage.setItem('companyId', data.companyId)
          resolve()
        }).catch(error => {
          reject(error)
        })
      })
    },
    CommitUser({ commit }, data){
      localStorage.setItem('loginUser', JSON.stringify(data))
      commit('SET_TOKEN', data.token)
      commit('SET_NAME', data.name || data.firstname)
      commit('SET_AVATAR', 'https://wpimg.wallstcn.com/f778738c-e4f8-4870-b634-56703b4acafe.gif')
      commit('SET_MOBILE', data.username)
      commit('SET_COMPANY', data.companyId)
      commit('SET_ROLES', data.authorities)
    },
    // 获取用户信息
    GetInfo({ commit, state }) {
      return new Promise((resolve, reject) => {
        getInfo(state.token, state.mobile || localStorage.getItem('username')).then(response => {
          const data = response.data
          commit('SET_ROLES', data.authorities)
          commit('SET_NAME', data.name || data.firstname)
          commit('SET_AVATAR', 'https://wpimg.wallstcn.com/f778738c-e4f8-4870-b634-56703b4acafe.gif')
          commit('SET_MOBILE', data.mobile)
          commit('SET_COMPANY', data.companyId)
          localStorage.setItem('companyId', data.companyId)
          resolve(response)
        }).catch(error => {
          console.log('error' + error)
          reject(error)
        })
      })
    },

    // 登出
    LogOut({ commit, state }) {
      return new Promise(resolve => {
        commit('SET_TOKEN', '')
        removeToken()
        resolve()
      })
    },

    // 前端 登出
    FedLogOut({ commit }) {
      return new Promise(resolve => {
        commit('SET_TOKEN', '')
        removeToken()
        resolve()
      })
    }
  }
}

export default user
